/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.trick.admin.controller;

import it.trick.admin.App;
import it.trick.admin.binding.SettingChannelProgressBinding;
import it.trick.admin.binding.UpdateChannelButtonBinding;
import it.trick.admin.model.ChannelModel;
import it.trick.admin.task.GetChannelModelTask;
import it.trick.admin.task.UpdateChannelTask;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;

/**
 * FXML Controller class
 *
 * @author takahara
 */
public class SettingChannelController extends AnchorPane implements Initializable {

    @FXML
    private TextField nameTextField;
    @FXML
    private TextArea descriptionTextArea;
    @FXML
    private TextArea contentsTextArea;
    @FXML
    public HBox progressIndicatorHBox;
    @FXML
    public Button updateButton;

    // -----------------------------タスク---------------------------------------
    public GetChannelModelTask getChannelModelTask = new GetChannelModelTask();
    public UpdateChannelTask updateChannelTask = new UpdateChannelTask();

    private ChannelModel channelModel;

    /**
     * コンストラクタ
     */
    public SettingChannelController(ChannelModel channelModel) {
        this.channelModel = channelModel;
        init();
    }

    /**
     * コントロールの初期化
     */
    private void init() {

        // ---------------------------------------------------
        // FXMLのロードと設定
        // ---------------------------------------------------
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource("resource/fxml/setting_channel_view.fxml"));
        fxmlLoader.setRoot(this);
        // 自分自身をコントロールとして設定
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        progressIndicatorHBox.visibleProperty().bind(
                new SettingChannelProgressBinding(
                getChannelModelTask,
                updateChannelTask));

        updateButton.disableProperty().bind(
                new UpdateChannelButtonBinding(
                nameTextField,
                descriptionTextArea,
                contentsTextArea));

        // ---------------------------------------------------------------------
        // サービス
        // ---------------------------------------------------------------------
        getChannelModelTask.setup(channelModel.getKeyValueString());
        getChannelModelTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent t) {

                // サービスの処理結果を受け取る
                ChannelModel channelModel = (ChannelModel) t.getSource().getValue();

                nameTextField.setText(channelModel.getName());
                if (channelModel.getDescription() == null) {
                    descriptionTextArea.setText("");
                } else {
                    descriptionTextArea.setText(channelModel.getDescription());
                }

                if (channelModel.getContent() == null) {
                    contentsTextArea.setText("");
                } else {
                    contentsTextArea.setText(channelModel.getContent());
                }
            }
        });

        getChannelModelTask.restart();
    }

    /**
     * キャンセルアクション
     */
    @FXML
    protected void cancelAction() {
        App.getInstance().closePopStage();
    }

    @FXML
    public void updateAction() {
        channelModel.setName(nameTextField.getText());
        channelModel.setDescription(descriptionTextArea.getText());
        channelModel.setContent(contentsTextArea.getText());

        updateChannelTask.setup(channelModel);
        updateChannelTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent t) {

                App.getInstance().closePopStage();
            }
        });

        updateChannelTask.restart();
    }
}
